
// check whether brackets are passed on correctly:
levelsof brackets, local(stufen)
foreach s of local stufen {
display "The lower bound of this bracket is `s'"
}

sum brackets
local max = `r(max)'

*1) Generate total number of tax units t_s above each threshold s:
  gen t_`max' = tu`max'
  label var t_`max' "Total number of tax units with wealth above `max''000 SFr."

*2.A) Generate total wealth w_s above each threshold s
  gen w_`max' = wea`max'
  label var w_`max' "Total wealth of all tax units with wealth above `max''000 SFr. (in 1000 CHF)"



local n : word count $b

forvalues i = 1/`n' {
local aa : word `i' of $a
local bb : word `i' of $b


    *1) Generate total number of tax units t_s above each threshold s:
         gen t_`bb' = tu`bb' + t_`aa'
         label var t_`bb' "Total number of tax units with wealth above `bb''000 SFr."
    *2.A) Generate total wealth w_s above each threshold s
         gen w_`bb' = wea`bb' + w_`aa'
         label var w_`bb' "Total wealth of all tax units with wealth above `bb''000 SFr. (in 1000 CHF)"
}
  



levelsof brackets, local(stufen)
foreach s of local stufen {
 * 3.A) Generate the average wealth w_s above each threshold s: 
  gen avg_`s'=w_`s'/t_`s'
  label var avg_`s' "Average wealth of all tax units with wealth above `s''000 SFr. (in 1000 CHF)"

 * 4) Calculate the cumulated population shares p_s above each threshold s:
   gen p_`s'=t_`s'/total_taxunits
   label var p_`s' "Share of tax units with wealth above `s''000 SFr."

 * 5.A) Calculate the local paretian distribution parameters b_s, a_s, k_s for each threshold s:
 * 5.1.A) calculate b
 gen b_`s'= avg_`s'/(`s'*1000)
 label var b_`s' "Local beta parameter for wealth > `s''000 SFr."
 
 ** 5.2.A) calculate a
 gen a_`s' = b_`s'/(b_`s'-1)
 label var a_`s' "Local alpha parameter for wealth > `s''000 SFr."
 
 *** 5.3.A) calculate k
 gen k_`s' = `s'*1000*p_`s'^(1/a_`s')
 label var k_`s' "Local k parameter for wealth > `s''000 SFr."

}
  

* * * * * * * * * * * * * * * * * * * * *
*Check where we extrapolate Top10% and Top0.01% instead of interpolating
sum brackets
local max = `r(max)'
local min = `r(min)'

cd "$mypath/output/"
mkdir tws
cd "$mypath/output/tws"

log using extrapolated_years_$dataset, replace
foreach s in  `min' `max' {
gen pop_`s'=t_`s'/total_taxunits
 }
 
  
display "EXTARPOLATION OF TOP 1%"
 list cantonid year pop_`max' if pop_`max'>0.01 & pop_`max' <.
 
display "EXTARPOLATION OF TOP 0.1%"
 list cantonid year pop_`max' if pop_`max'>0.001 &pop_`max' <.
 
display "EXTARPOLATION OF TOP 0.01% in addition to extrapolation top 0.1%"
 list cantonid year pop_`max' if pop_`max'>0.0001 & pop_`max' < 0.001
 
 
 display "EXTARPOLATION OF TOP 10%"
 list cantonid year pop_`min' if pop_`min'<0.1

foreach s in `min' `max'  {
drop pop_`s'
 }
log close
* * * * * * * * * * * * * * * * * * * * *

*    ** top 10% **    * *    ** top 10% **    * *    ** top 10% **    * *    ** top 10% **    *

gen ts90 =  0
label var ts90 "wealth threshold to belong to the top10% (in 1000 CHF)"

gen wea_avg90 = 0 
label var wea_avg90 "Average wealth of the top10% (i.e. above S90) (in 1000 CHF)"

gen wea_abv90 = 0 
label var wea_abv90 "Total wealth of the top10% (i.e. above S90) (in 1000 CHF)"

gen b90 = 0
label var b90 "beta-coefficient of the top10%"

local list
levelsof brackets, local(stufen)
foreach s of local stufen {
local list `list' abs90_`s', 

* create the absolute deviation from the 10%-population share for each wealth bracket _`s'
gen abs90_`s'= abs(p_`s'- 0.1)
}


* create an indicator (dummy) indicating the bracket where p_`s' is closest to 10% 
levelsof brackets, local(stufen)
foreach s of local stufen {
gen bracket90_`s'= min(`list' 999999)
}
levelsof brackets, local(stufen)
foreach s of local stufen {
replace bracket90_`s' = 0 if bracket90_`s' !=abs90_`s'
replace bracket90_`s' = 1 if bracket90_`s'==abs90_`s'

*drop the top10% absolute deviation variables
//drop abs90_`s'


*calculate the top10% wealth threshold ts90:
gen ts90_`s' = (k_`s'/0.1^(1/a_`s'))*bracket90_`s'
replace  ts90_`s' = 0 if ts90_`s'==.
}

levelsof brackets, local(stufen)
foreach s of local stufen {
replace ts90 = ts90 + ts90_`s'
}

levelsof brackets, local(stufen)
foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg90_`s' = ts90_`s'*b_`s'*bracket90_`s'
replace wea_avg90_`s' = 0 if wea_avg90_`s'==.
}

levelsof brackets, local(stufen)
foreach s of local stufen {
replace wea_avg90 = wea_avg90 + wea_avg90_`s'
}

*calculate the total wealth above that threshold
levelsof brackets, local(stufen)
foreach s of local stufen {
gen wea_abv90_`s' = ts90_`s'*b_`s'*bracket90_`s'*0.1*total_taxunits
replace wea_abv90_`s'=0 if wea_abv90_`s'==.
}
levelsof brackets, local(stufen)
foreach s of local stufen {
replace wea_abv90 =wea_abv90 + wea_abv90_`s' 
}


*generate the beta coefficients for the top 10%
levelsof brackets, local(stufen)
foreach s of local stufen {
gen b90_`s' = b_`s'*bracket90_`s'
replace b90_`s'=0 if b90_`s'==.
}

levelsof brackets, local(stufen)
foreach s of local stufen {
replace b90 = b90 + b90_`s'
}


* calculate the top10% wealth share
gen P90= wea_abv90/wea_total
label var P90 "Top 10% wealth share on uncorrected total tax wealth"

* calculate the top10% wealth share on imputed tax wealth
cap gen P90d= wea_abv90/wealth_denominator
cap label var P90d "Top 10% wealth share on imputed tax wealth"



*    ** top 5% **    * *    ** top 5% **    * *    ** top 5% **    * *    ** top 5% **    *

gen ts95 =  0
label var ts95 "wealth threshold to belong to the top5%"

gen wea_avg95 = 0 
label var wea_avg95 "Average wealth of the top5% (i.e. above S95)"

gen wea_abv95 = 0 
label var wea_abv95 "Total wealth of the top5% (i.e. above S95)"

gen b95 = 0
label var b95 "beta-coefficient of the top5%"

local list
foreach s of local stufen {
local list `list' abs95_`s', 
* create the absolute deviation from the 5%-population share for each wealth bracket _`s'
gen abs95_`s'= abs(p_`s'- 0.05)
}


foreach s of local stufen {
* create an indicator (dummy) indicating the bracket where p_`s' is closest to 5% 
gen bracket95_`s'=min(`list' 999999)
}

foreach s of local stufen {
replace bracket95_`s' = 0 if bracket95_`s' !=abs95_`s'
replace bracket95_`s' = 1 if bracket95_`s'==abs95_`s'

*drop the top5% absolute deviation variables
drop abs95_`s'


*calculate the top5% wealth threshold ts95:
gen ts95_`s' = (k_`s'/0.05^(1/a_`s'))*bracket95_`s'
replace  ts95_`s' = 0 if ts95_`s'==.
}

foreach s of local stufen {
replace ts95 = ts95 + ts95_`s'
}

foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg95_`s' = ts95_`s'*b_`s'*bracket95_`s'
replace wea_avg95_`s' = 0 if wea_avg95_`s'==.
}

foreach s of local stufen {
replace wea_avg95 = wea_avg95 + wea_avg95_`s'
}

*calculate the total wealth above that threshold
foreach s of local stufen {
gen wea_abv95_`s' = ts95_`s'*b_`s'*bracket95_`s'*0.05*total_taxunits
replace wea_abv95_`s'=0 if wea_abv95_`s'==.
}
foreach s of local stufen {
replace wea_abv95 =wea_abv95 + wea_abv95_`s' 
}


*generate the beta coeffiecients for the top 5%
foreach s of local stufen {
gen b95_`s' = b_`s'*bracket95_`s'
replace b95_`s'=0 if b95_`s'==.
}

foreach s of local stufen {
replace b95 = b95 + b95_`s'
}


* calculate the top5% wealth share
gen P95= wea_abv95/wea_total
label var P95 "Top 5% wealth share on uncorrected total tax wealth"

* calculate the top5% wealth share on imputed tax wealth
cap gen P95d= wea_abv95/wealth_denominator
cap label var P95d "Top 5% wealth share on imputed tax wealth"




*    ** top 1% **    * *    ** top 1% **    * *    ** top 1% **    * *    ** top 1% **    *

gen ts99 =  0
label var ts99 "wealth threshold to belong to the top1%"

gen wea_avg99 = 0 
label var wea_avg99 "Average wealth of the top1% (i.e. above S99)"

gen wea_abv99 = 0 
label var wea_abv99 "Total wealth of the top1% (i.e. above S99)"

gen b99 = 0
label var b99 "beta-coefficient of the top1%"

local list
foreach s of local stufen {
local list `list' abs99_`s', 
* create the absolute deviation from the 1%-population share for each wealth bracket _`s'
gen abs99_`s'= abs(p_`s'- 0.01)
}


foreach s of local stufen {
* create an indicator (dummy) indicating the bracket where p_`s' is closest to 1% 
gen bracket99_`s'=min(`list' 999999)
}

foreach s of local stufen {
replace bracket99_`s' = 0 if bracket99_`s' !=abs99_`s'
replace bracket99_`s' = 1 if bracket99_`s'==abs99_`s'

*drop the top1% absolute deviation variables
drop abs99_`s'


*calculate the top1% wealth threshold ts99:
gen ts99_`s' = (k_`s'/0.01^(1/a_`s'))*bracket99_`s'
replace  ts99_`s' = 0 if ts99_`s'==.
}

foreach s of local stufen {
replace ts99 = ts99 + ts99_`s'
}

foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg99_`s' = ts99_`s'*b_`s'*bracket99_`s'
replace wea_avg99_`s' = 0 if wea_avg99_`s'==.
}

foreach s of local stufen {
replace wea_avg99 = wea_avg99 + wea_avg99_`s'
}

*calculate the total wealth above that threshold
foreach s of local stufen {
gen wea_abv99_`s' = ts99_`s'*b_`s'*bracket99_`s'*0.01*total_taxunits
replace wea_abv99_`s'=0 if wea_abv99_`s'==.
}
foreach s of local stufen {
replace wea_abv99 =wea_abv99 + wea_abv99_`s' 
}


*generate the beta coeffiecients for the top 1%
foreach s of local stufen {
gen b99_`s' = b_`s'*bracket99_`s'
replace b99_`s'=0 if b99_`s'==.
}

foreach s of local stufen {
replace b99 = b99 + b99_`s'
}


* calculate the top1% wealth share
gen P99= wea_abv99/wea_total
label var P99 "Top 1% wealth share on uncorrected total tax wealth"

* calculate the top1% wealth share on imputed tax wealth
cap gen P99d= wea_abv99/wealth_denominator
cap label var P99d "Top 1% wealth share on imputed tax wealth"





*    ** top 0.5% **    * *    ** top 0.5% **    * *    ** top 0.5% **    * *    ** top 0.5% **    *

gen ts995 =  0
label var ts995 "wealth threshold to belong to the top0.5%"

gen wea_avg995 = 0 
label var wea_avg995 "Average wealth of the top0.5% (i.e. above S995)"

gen wea_abv995 = 0 
label var wea_abv995 "Total wealth of the top0.5% (i.e. above S995)"

gen b995 = 0
label var b995 "beta-coefficient of the top0.5%"

local list
foreach s of local stufen {
local list `list' abs995_`s', 
* create the absolute deviation from the 0.5%-population share for each wealth bracket _`s'
gen abs995_`s'= abs(p_`s'- 0.005)
}


foreach s of local stufen {
* create an indicator (dummy) indicating the bracket where p_`s' is closest to 0.5% 
gen bracket995_`s'=min(`list' 999999)
}

foreach s of local stufen {
replace bracket995_`s' = 0 if bracket995_`s' !=abs995_`s'
replace bracket995_`s' = 1 if bracket995_`s'==abs995_`s'

*drop the top0.5% absolute deviation variables
drop abs995_`s'


*calculate the top0.5% wealth threshold ts995:
gen ts995_`s' = (k_`s'/0.005^(1/a_`s'))*bracket995_`s'
replace  ts995_`s' = 0 if ts995_`s'==.
}

foreach s of local stufen {
replace ts995 = ts995 + ts995_`s'
}

foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg995_`s' = ts995_`s'*b_`s'*bracket995_`s'
replace wea_avg995_`s' = 0 if wea_avg995_`s'==.
}

foreach s of local stufen {
replace wea_avg995 = wea_avg995 + wea_avg995_`s'
}

*calculate the total wealth above that threshold
foreach s of local stufen {
gen wea_abv995_`s' = ts995_`s'*b_`s'*bracket995_`s'*0.005*total_taxunits
replace wea_abv995_`s'=0 if wea_abv995_`s'==.
}
foreach s of local stufen {
replace wea_abv995 =wea_abv995 + wea_abv995_`s' 
}


*generate the beta coeffiecients for the top 0.5%
foreach s of local stufen {
gen b995_`s' = b_`s'*bracket995_`s'
replace b995_`s'=0 if b995_`s'==.
}

foreach s of local stufen {
replace b995 = b995 + b995_`s'
}


* calculate the top0.5% wealth share
gen P995= wea_abv995/wea_total
label var P995 "Top 0.5% wealth share on uncorrected total tax wealth"

* calculate the top0.5% wealth share on imputed tax wealth
cap gen P995d= wea_abv995/wealth_denominator
cap label var P995d "Top 0.5% wealth share on imputed tax wealth"





*    ** top 0.1% **    * *    ** top 0.1% **    * *    ** top 0.1% **    * *    ** top 0.1% **    *

gen ts999 =  0
label var ts999 "wealth threshold to belong to the top0.1%"

gen wea_avg999 = 0 
label var wea_avg999 "Average wealth of the top0.1% (i.e. above S999)"

gen wea_abv999 = 0 
label var wea_abv999 "Total wealth of the top0.1% (i.e. above S999)"

gen b999 = 0
label var b999 "beta-coefficient of the top0.1%"

local list
foreach s of local stufen {
local list `list' abs999_`s', 
* create the absolute deviation from the 0.1%-population share for each wealth bracket _`s'
gen abs999_`s'= abs(p_`s'- 0.001)
}


foreach s of local stufen {
* create an indicator (dummy) indicating the bracket where p_`s' is closest to 0.1% 
gen bracket999_`s'=min(`list' 999999)
}

foreach s of local stufen {
replace bracket999_`s' = 0 if bracket999_`s' !=abs999_`s'
replace bracket999_`s' = 1 if bracket999_`s'==abs999_`s'

*drop the top0.1% absolute deviation variables
drop abs999_`s'


*calculate the top0.1% wealth threshold ts999:
gen ts999_`s' = (k_`s'/0.001^(1/a_`s'))*bracket999_`s'
replace  ts999_`s' = 0 if ts999_`s'==.
}

foreach s of local stufen {
replace ts999 = ts999 + ts999_`s'
}

foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg999_`s' = ts999_`s'*b_`s'*bracket999_`s'
replace wea_avg999_`s' = 0 if wea_avg999_`s'==.
}

foreach s of local stufen {
replace wea_avg999 = wea_avg999 + wea_avg999_`s'
}

*calculate the total wealth above that threshold
foreach s of local stufen {
gen wea_abv999_`s' = ts999_`s'*b_`s'*bracket999_`s'*0.001*total_taxunits
replace wea_abv999_`s'=0 if wea_abv999_`s'==.
}
foreach s of local stufen {
replace wea_abv999 =wea_abv999 + wea_abv999_`s' 
}


*generate the beta coeffiecients for the top 0.1%
foreach s of local stufen {
gen b999_`s' = b_`s'*bracket999_`s'
replace b999_`s'=0 if b999_`s'==.
}

foreach s of local stufen {
replace b999 = b999 + b999_`s'
}


* calculate the top0.1% wealth share
gen P999= wea_abv999/wea_total
label var P999 "Top 0.1% wealth share on uncorrected total tax wealth"

* calculate the top0.1% wealth share on imputed tax wealth
cap gen P999d= wea_abv999/wealth_denominator
cap label var P999d "Top 0.1% wealth share on imputed tax wealth"




*    ** top 0.01% **    * *    ** top 0.01% **    * *    ** top 0.01% **    * *    ** top 0.01% **    *

gen ts9999 =  0
label var ts9999 "wealth threshold to belong to the top0.01%"

gen wea_avg9999 = 0 
label var wea_avg9999 "Average wealth of the top0.01% (i.e. above S9999)"

gen wea_abv9999 = 0 
label var wea_abv9999 "Total wealth of the top0.01% (i.e. above S9999)"

gen b9999 = 0
label var b9999 "beta-coefficient of the top0.01%"

local list
foreach s of local stufen {
local list `list' abs9999_`s', 
* create the absolute deviation from the 0.01%-population share for each wealth bracket _`s'
gen abs9999_`s'= abs(p_`s'- 0.0001)
}


foreach s of local stufen {
* create an indicator (dummy) indicating the bracket where p_`s' is closest to 0.01% 
gen bracket9999_`s'=min(`list' 999999)
}

foreach s of local stufen {
replace bracket9999_`s' = 0 if bracket9999_`s' !=abs9999_`s'
replace bracket9999_`s' = 1 if bracket9999_`s'==abs9999_`s'

*drop the top0.01% absolute deviation variables
drop abs9999_`s'


*calculate the top0.01% wealth threshold ts9999:
gen ts9999_`s' = (k_`s'/0.0001^(1/a_`s'))*bracket9999_`s'
replace  ts9999_`s' = 0 if ts9999_`s'==.
}

foreach s of local stufen {
replace ts9999 = ts9999 + ts9999_`s'
}

foreach s of local stufen {
*calculate the average wealth above that threshold
gen wea_avg9999_`s' = ts9999_`s'*b_`s'*bracket9999_`s'
replace wea_avg9999_`s' = 0 if wea_avg9999_`s'==.
}

foreach s of local stufen {
replace wea_avg9999 = wea_avg9999 + wea_avg9999_`s'
}

*calculate the total wealth above that threshold
foreach s of local stufen {
gen wea_abv9999_`s' = ts9999_`s'*b_`s'*bracket9999_`s'*0.0001*total_taxunits
replace wea_abv9999_`s'=0 if wea_abv9999_`s'==.
}
foreach s of local stufen {
replace wea_abv9999 =wea_abv9999 + wea_abv9999_`s' 
}


*generate the beta coeffiecients for the top 0.01%
foreach s of local stufen {
gen b9999_`s' = b_`s'*bracket9999_`s'
replace b9999_`s'=0 if b9999_`s'==.
}

foreach s of local stufen {
replace b9999 = b9999 + b9999_`s'
}


* calculate the top0.01% wealth share
gen P9999= wea_abv9999/wea_total
label var P9999 "Top 0.01% wealth share on uncorrected total tax wealth"

* calculate the top0.01% wealth share on imputed tax wealth
cap gen P9999d= wea_abv9999/wealth_denominator
cap label var P9999d "Top 0.01% wealth share on imputed tax wealth"





*** *** *** *** *** *** *** 
* SHARES WITHIN SHARES *

gen top10_within_top1 =wea_abv999/wea_abv99
label var top10_within_top1 "Top 10% within the top 1% wealth share"

gen top1_within_top1 =wea_abv9999/wea_abv99
label var top1_within_top1 "Top 1% within the top 1% wealth share"

gen top10_within_top10 =wea_abv99/wea_abv90
label var top10_within_top10 "Top 10% within the top 10% wealth share"


// in-between wealth shares on uncorrected total wealth
* top 10-5% shares
gen P10_5 = P90-P95
label var P10_5 "Top 10-5% wealth share on uncorrected total tax wealth"

* top 5-1% shares
gen P5_1 = P95-P99
label var P5_1 "Top 5-1% wealth share on uncorrected total tax wealth"

* top 10-1% shares
gen P10_1 = P90-P99
label var P10_1 "Top 10-1% wealth share on uncorrected total tax wealth"

* top 1-0.5% shares
gen P1_05 = P99-P995
label var P1_05 "Top 1-0.5% wealth share on uncorrected total tax wealth"

* top 1-0.1% shares
gen P1_01 = P99-P999
label var P1_01 "Top 1-0.1% wealth share on uncorrected total tax wealth"

*Top 0.5-0.1% wealth share	
gen P05_01 = P995-P999
label var P05_01 "Top 0.5-0.1% wealth share on uncorrected total tax wealth"

*Top 0.1-0.01% wealth share
gen P01_001 = P999-P9999
label var P01_001 "Top 0.1-0.01% wealth share on uncorrected total tax wealth"


**** **** **** **** ****
* ALPHA AND BETA COEFFICIENTS 

gen alpha=1/(1-log(P99/P999)/log(10))
label var alpha "Pareto coefficient alpha"
note alpha: The Pareto-Lorenz a coefficients were computed using the top shares estimates. As a rule they were estimated from the top 0.1% share within the top 1% share: a=1/[1-log(Share1%/Share0.1%)/log(10)].

gen beta=alpha/(alpha-1)
label var beta "Inverted Pareto coefficient beta"
note beta: The inverted Pareto-Lorenz b coefficients were computed from the Pareto-Lorenz coefficients using the formula b=a/(a-1). Alternatively they can be computed by using directly the top wealth shares series and the formula: b=1/[log(Share1%/Share0.1%)/log(10)].

/*
gen alpha_d=1/(1-log(P99d/P999d)/log(10))
label var alpha_d "Pareto coefficient alpha"
note alpha_d: The Pareto-Lorenz a coefficients were computed using the top shares estimates. As a rule they were estimated from the top 0.1% share within the top 1% share: a=1/[1-log(Share1%/Share0.1%)/log(10)].

gen beta_d=alpha_d/(alpha_d-1)
label var beta_d "Inverted Pareto coefficient beta"
note beta_d: The inverted Pareto-Lorenz b coefficients were computed from the Pareto-Lorenz coefficients using the formula b=a/(a-1). Alternatively they can be computed by using directly the top wealth shares series and the formula: b=1/[log(Share1%/Share0.1%)/log(10)].
*/
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 

  
